// pages/myinfo/myinfo.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    photo:'',
    genderarr: [{ code: 1, text: '男' },{ code: 2, text: '女' }],
    indexforgender:0,
    birthday:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      data: {},
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        //console.log("个人信息：" + data)
        if (data.code == 200) {
          let gender = data.data.gender;
          let index=0;
          if(gender==1){
            index=0
          } else if(gender==2){
            index=1
          }
          self.setData({ userInfo: data.data, photo: data.data.headImg, birthday: data.data.birthday, indexforgender:index})
        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none'
          })
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeHead: function () {
    let self = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.reqPath + 'shop/upload',
          filePath: tempFilePaths[0],
          name: 'files[]',
          header: {
            'authorization': app.globalData.authorization
          },
          formData: {},
          success: function ({ data }) {
            data = JSON.parse(data)
            console.log(data)
            if (data.code == 200) {
              let photo = data.data[0]; 
              self.setData({ photo:data.data})
              wx.request({
                url: app.globalData.reqPath +'shop/MembersSafe',
                data:{
                  headImg:photo
                },
                method:'POST',
                header: {
                  'authorization': app.globalData.authorization
                },
                success:function({data}){

                }
              })

            }
          }
        })
      },
    })
  },
  editnick:function(){//修改昵称

  },
  chagegender:function(e){
    //  console.log(e.detail)
    this.setData({indexforgender:e.detail.value});  
  },
  changebirth:function(e){
    console.log()
    this.setData({birthday:e.detail.value})
  },
  confirm:function(e){
    let self=this;
    console.log(e)
    let params=e.detail.value;
    let data={
      nickname:params.nickname,
      idCard: params.idCard =='未设置'?'':params.idCard,
      birthday:this.data.birthday,
      gender: this.data.genderarr[this.data.indexforgender].code,
      profile: params.profile == '未设置' ? '' : params.profile,
    }
    wx.request({
      url:app.globalData.reqPath + 'shop/MembersSafe',
      data:data,
      method: 'POST',
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data }) {
          if(data.code==200){
            wx.showToast({
              title: '保存成功',
              icon:'none'
            })
            setTimeout(function () { self.onLoad();},300)
            
          }
      } 
    })
  },
  idCardfoucus:function(e){
     e.detail.value==''
  }
})